#!/usr/bin/env python3
"""
简化版后端启动脚本 - 暂时跳过Elasticsearch
"""

from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from views import router as auth_router
from models import create_tables, init_tags
import uvicorn

# 创建FastAPI应用
app = FastAPI(
    title="趣享生活 API",
    description="小红书原型项目 - 用户登录模块 + 内容推荐系统（简化版）",
    version="2.0.0"
)

# 配置CORS
app.add_middleware(
    CORSMiddleware,
    allow_origins=["http://localhost:3000", "http://localhost:5173"],  # 前端地址
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

# 注册路由
app.include_router(auth_router, prefix="/api/auth", tags=["认证"])
app.include_router(auth_router, prefix="/api", tags=["标签"])

# 创建数据库表
create_tables()

# 初始化标签数据
init_tags()

@app.get("/")
async def root():
    return {
        "message": "趣享生活 API 服务运行中",
        "version": "2.0.0",
        "docs": "/docs",
        "features": ["用户认证", "兴趣标签", "内容推荐（开发中）"],
        "status": "简化版 - Elasticsearch功能暂时禁用"
    }

@app.get("/health")
async def health_check():
    return {"status": "healthy"}

# 添加一个临时的推荐接口，返回模拟数据
@app.post("/api/recommendations")
async def get_recommendations(request: dict):
    """临时推荐接口 - 返回模拟数据"""
    return {
        "success": True,
        "message": "推荐获取成功（模拟数据）",
        "data": {
            "recommendations": [
                {
                    "content_id": 1,
                    "title": "美食分享：红烧肉的制作方法",
                    "description": "今天分享一道美味的红烧肉，简单易做，营养丰富。制作时要注意火候控制，这样口感会更好。",
                    "author_nickname": "美食达人",
                    "tag_names": ["美食", "烹饪", "家常菜"],
                    "first_image_url": "https://picsum.photos/400/300?random=1",
                    "like_count": 156,
                    "comment_count": 23,
                    "view_count": 1200,
                    "published_at": "2024-01-15T10:30:00",
                    "score": 0.95
                },
                {
                    "content_id": 2,
                    "title": "旅行攻略：杭州的必去景点",
                    "description": "刚刚从杭州回来，整理了一份详细的旅行攻略。这里的风景真的很美，值得一去。",
                    "author_nickname": "旅行者",
                    "tag_names": ["旅行", "攻略", "景点"],
                    "first_image_url": "https://picsum.photos/400/300?random=2",
                    "like_count": 89,
                    "comment_count": 15,
                    "view_count": 800,
                    "published_at": "2024-01-14T15:20:00",
                    "score": 0.88
                },
                {
                    "content_id": 3,
                    "title": "生活小贴士：如何快速整理房间",
                    "description": "分享一个实用的生活小贴士，如何快速整理房间。这个方法真的很实用，推荐给大家。",
                    "author_nickname": "生活达人",
                    "tag_names": ["生活", "小贴士", "实用"],
                    "first_image_url": "https://picsum.photos/400/300?random=3",
                    "like_count": 67,
                    "comment_count": 12,
                    "view_count": 600,
                    "published_at": "2024-01-13T09:15:00",
                    "score": 0.82
                }
            ],
            "page": request.get("page", 1),
            "size": request.get("size", 20)
        },
        "total": 3,
        "page": request.get("page", 1),
        "size": request.get("size", 20)
    }

@app.post("/api/recommendations/behavior")
async def record_behavior(content_id: int, behavior_type: str):
    """临时行为记录接口"""
    return {
        "success": True,
        "message": "用户行为记录成功",
        "data": {
            "content_id": content_id,
            "behavior_type": behavior_type,
            "timestamp": "2024-01-15T10:30:00"
        }
    }

if __name__ == "__main__":
    print("=== 趣享生活后端服务启动（简化版）===")
    print("服务地址: http://localhost:8000")
    print("API文档: http://localhost:8000/docs")
    print("健康检查: http://localhost:8000/health")
    print("推荐功能: http://localhost:8000/api/recommendations")
    print("注意: 当前为简化版，Elasticsearch功能暂时禁用")
    print("按 Ctrl+C 停止服务")
    print("=" * 40)
    
    uvicorn.run(
        "main_simple:app",
        host="0.0.0.0",
        port=8000,
        reload=True,
        log_level="info"
    )
